import React from 'react';
import { Form } from '@ant-design/compatible';
import '@ant-design/compatible/assets/index.css';
import { Row, Col, Input, Modal, message } from 'antd';
const { TextArea } = Input;
const confirm = Modal.confirm;
const FormItem = Form.Item;
import { connect } from 'dva';
import { ModalC, ProductGroupTreeC, SecuritiesCodeC, InputNumberC, SelectC, codeType } from '../../../../comps';

@Form.create()
class adjustC extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      value: '', // 模态框标题
      userName: '', // 登录人名称
    };
  }

  componentDidMount() {
    this.props.dispatch({
      type: 'positionManageM/getUser',
      payload: localStorage.getItem('antd-pro-superManId'),
      callback: (data) => {
        if (data) {
          this.setState({
            userName: data.data.name,
          });
        }
      },
    });
  }

  // 模态框内容
  addForm = () => {
    const { getFieldDecorator } = this.props.form;
    const {
      tableData,
      type,
      globalM: {
        dataDictionary: { ENTRUSTED_INVESTMENT_HOLD_ATTRCODE = [] },
      },
    } = this.props;
    const formItemLayout = {
      labelCol: {
        span: 6,
      },
      wrapperCol: {
        span: 18,
      },
    };
    const formItemLayouts = {
      labelCol: {
        span: 3,
      },
      wrapperCol: {
        span: 18,
      },
    };
    let shortBlankSignData = [];

    ENTRUSTED_INVESTMENT_HOLD_ATTRCODE.forEach((i) => {
      shortBlankSignData.push({ title: i.value, value: i.code });
    });
    let maxAdjustNum;
    if (type == 1 || type == 2) {
      maxAdjustNum = tableData.availableAmount;
    } else if (type == 3) {
      maxAdjustNum = tableData.pledgeAmount;
    } else {
      maxAdjustNum = 999999999999999999999;
    }
    return (
      <div>
        <Form>
          {/* ***************************第一行************************************* */}
          <Row>
            <Col span={12}>
              <ProductGroupTreeC
                form={this.props.form}
                dataIndex='fundId'
                title='产品组合'
                authority={'write'}
                initialValue={tableData ? tableData.combiId : undefined}
                formItemLayout={formItemLayout}
                disabled={true}
                multiple={false}
              />
            </Col>
            <Col span={12}>
              <SecuritiesCodeC
                form={this.props.form}
                dataIndex='zqcode'
                title='证券代码'
                initialValue={tableData ? `${tableData.id.tradingcode}-${tableData.secuabbr}` : undefined}
                extraLayout={formItemLayout}
                disabled={true}
              />
            </Col>
          </Row>
          {/* ***************************第二行************************************* */}
          <Row>
            {/* {tableData?.id?.securityType == '23' || tableData?.id?.securityType == '24' ? <Col span={12}>
              <SelectC
                title='多空标志'
                dataIndex='shortBlankSign'
                placeholder="请输入"
                form={this.props.form}
                initialValue={tableData?.id?.securityType == '23' ? '1' : '2'}
                formItemLayout={formItemLayout}
                dataSource={[{ value: '1', title: "多头" }, { value: '2', title: "空头" }]}
              />
            </Col> :
            </Col>} */}
            <Col span={12}>
              <SelectC
                title='交易属性'
                dataIndex='shortBlankSign'
                form={this.props.form}
                formItemLayout={formItemLayout}
                initialValue={tableData ? tableData.shortBlankSign : undefined}
                dataSource={shortBlankSignData}
                disabled={true}
              />
            </Col>
            <Col span={12}>
              <InputNumberC
                title='可用数量'
                dataIndex='availableAmount'
                placeholder='请输入'
                form={this.props.form}
                formItemLayout={formItemLayout}
                initialValue={tableData ? tableData.availableAmount : undefined}
                disabled={true}
              />
            </Col>
          </Row>
          {/* ***************************第三行************************************* */}
          <Row>
            {tableData?.id?.securityType == '0' || tableData?.id?.securityType == '7' ? (
              <>
                <Col span={12}>
                  <InputNumberC
                    title='净价'
                    dataIndex='price' //净价 就用成本价的变量名
                    placeholder='请输入'
                    precision={4}
                    min={1}
                    max={999999999999}
                    form={this.props.form}
                    formItemLayout={formItemLayout}
                    rules={[{ required: true, message: `请输入净价` }]}
                  />
                </Col>
                <Col span={12}>
                  <InputNumberC
                    title='全价'
                    dataIndex='priceqj'
                    placeholder='请输入'
                    precision={4}
                    min={1}
                    max={999999999999}
                    form={this.props.form}
                    formItemLayout={formItemLayout}
                    rules={[{ required: true, message: `请输入全价` }]}
                  />
                </Col>
              </>
            ) : (
              <Col span={12}>
                <InputNumberC
                  title='成本价'
                  dataIndex='price'
                  placeholder='请输入'
                  precision={4}
                  min={1}
                  max={999999999999}
                  form={this.props.form}
                  formItemLayout={formItemLayout}
                  initialValue={tableData ? tableData.costPrice : undefined}
                  rules={[{ required: true, message: `请输入成本价` }]}
                />
              </Col>
            )}
            <Col span={12}>
              <InputNumberC
                title='调整数量'
                dataIndex='amount'
                placeholder='请输入'
                precision={0}
                min={1}
                max={99999999999}
                unit={'手'}
                form={this.props.form}
                formItemLayout={formItemLayout}
                rules={[
                  { required: true, message: `请输入调整数量` },
                  { pattern: /^[1-9]/, message: '数量不能小于1' },
                ]}
              />
            </Col>
          </Row>
          {/* ***************************第四行************************************* */}
          <Row>
            <Col span={24}>
              <FormItem label='备注' {...formItemLayouts}>
                {getFieldDecorator('remark')(<TextArea />)}
              </FormItem>
            </Col>
          </Row>
        </Form>
      </div>
    );
  };

  // 确认
  addanOK = () => {
    const {
      form: { validateFields, resetFields },
      tableData,
      type,
    } = this.props;
    validateFields(['remark', 'price', 'amount', 'priceqj', 'pricejj'], (err, fieldsValue) => {
      if (err) return;
      if (type == 1 || type == 2) {
        if (fieldsValue.amount > tableData.availableAmount) {
          message.warning('调整数量不能大于可用数量');
          return;
        }
      } else if (type == 3) {
        if (fieldsValue.amount > tableData.pledgeAmount) {
          message.warning('调整数量不能大于质押数量');
          return;
        }
      }

      let value = {
        ...fieldsValue,
        amount: fieldsValue.amount.toString(),
        dataDt: tableData.id.dataDt,
        exchangeCode: tableData.id.exchangeCode,
        miniUnitId: tableData.combiCode,
        operateBy: this.state.userName,
        operateType: type,
        securityType: tableData.id.securityType,
        tradingcode: tableData.id.tradingcode,
      };
      this.props.dispatch({
        type: 'positionManageM/adjustSecurity',
        payload: value,
        callback: (res) => {
          resetFields();
          this.props.changeVisible();
          this.props.onRefesh(); //刷新列表
        },
      });
    });
  };

  // 取消
  addanCancel = () => {
    const {
      form: { resetFields },
    } = this.props;
    this.props.changeVisible();
    resetFields();
  };

  render() {
    return (
      <ModalC
        title={this.props.value}
        visible={this.props.visible}
        maskClosable={false}
        onOk={this.addanOK}
        onCancel={this.addanCancel}
        confirmLoading={this.props.loading.effects['positionManageM/adjustSecurity'] === true}
        width={683}
      >
        <div>{this.addForm()}</div>
      </ModalC>
    );
  }
}
export default connect(({ positionManage, loading, globalM }) => ({ positionManage, loading, globalM }))(adjustC);
